package com.dq.Week1.day05.homeWork;

import java.util.Arrays;

public class UserUtils {

    User[] users = new User[]{};
    int num = 0;

    public boolean exist(String username) {
        for (int i = 0; i < num; i++) {
            if (username.equals(users[i].username)) {
                return true;
            }
        }
        return false;
    }

    public boolean register(String username, String password) {
        //判断用户是否存在
        if (exist(username)) {
            return false;
        }

        if (num == users.length) {
            users = Arrays.copyOf(users, users.length + 5);
        }
        //之前错了这里改正
        User user = new User(username,password);
        users[num] = user;


//        users[num].username = username;
//        users[num].password = password;

        num++;//计数器

        return true;
    }

    public boolean login(String username,String password) {
        for (int i = 0; i < num; i++) {
            //users[i] 表示user对象
            if(username.equals(users[i].username) && password.equals(users[i].password)) {
                return true;
            }
        }
        return false;
    }
}





